//func.h
#ifndef __FUNC_H__
#define __FUNC_H__

#include "head.h"

//接收发送缓冲区大小
#define BUF_SIZE 1024
//命令中最多有几个参数
#define MAX_WORDNUM 5
//命令一个参数的最大长度
#define MAX_WORDLEN 30
// 100M文件大小限制
#define HUNDRED_MEGABYTE 104857600

// 命令标签
typedef enum {
    TAG_REGISTER = 1,
    TAG_LOGIN,
    TAG_CANCEL,
    TAGCD,
    TAGLS,
    TAGREMOVE,
    TAGPWD,
    TAGMKDIR,
    TAGRMDIR,
    TAGGETS,
    TAGPUTS,
    TAG_RESPONSEREGISTER,
    TAG_RESPONSELOGIN,
    TAG_RESPONSECANCEL,
    TAG_RESPONSECD,
    TAG_RESPONSELS,
    TAG_RESPONSEREMOVE,
    TAG_RESPONSEPWD,
    TAG_RESPONSEMKDIR,
    TAG_RESPONSERMDIR,
    TAG_RESPONSEGETS,
    TAG_RESPONSEPUTS,
    TAG_ERROR,
} Tag;

typedef int socket_t;

// 共享资源结构体
typedef struct userInfo_s{
    int user_id;
    char username[64];
    char current_dir[256];
} userInfo_t;

typedef struct train_s {
    int tag;
    int data_length;
    char data[BUF_SIZE];
} train_t, *ptrain_t;

//命令类型
typedef struct {
    int argc;       //命令中有几个参数
    char cmd[MAX_WORDNUM][MAX_WORDLEN];
} cmd_t, *pcmd_t;

// 网络通信函数
int getValidStr(char *input, char *str);    
int getClientConf(char *fileconf, char *ip, char *port);
int epollAdd(int epfd, int fd);
int epollDel(int epfd, int fd);

// 用户认证函数
int user_register(socket_t fd_server, userInfo_t *userInfo);
int user_login(socket_t fd_server, userInfo_t *userInfo);
int user_cancel(socket_t fd_server, userInfo_t *userInfo);

// 命令处理函数
void init_cmd(char *line, pcmd_t pcmd);
void print_cmd(pcmd_t pcmd);
int cmd_check(pcmd_t pcmd, int *ptag);

// 目录操作函数
int handle_pwd(socket_t fd_server);
int handle_ls(socket_t fd_server);
int handle_cd(socket_t fd_server, pcmd_t pcmd);
int handle_mkdir(socket_t fd_server, pcmd_t pcmd);
int handle_rmdir(socket_t fd_server, pcmd_t pcmd);
int handle_remove(socket_t fd_server, pcmd_t pcmd);

// 文件传输函数
int handle_puts(socket_t fd_server, pcmd_t pcmd);
int handle_gets(socket_t fd_server, pcmd_t pcmd);
int send_file_train(int socket_fd, char *filename, off_t file_size);
int recv_file_train(int fd_socket, char *filename);
int get_filesize(char *filename);
char* calculate_file_hash(char *filename);

#endif
